using System;
using System.Collections.Generic;
using System.Collections;

using System.Text;
using System.Data;
using System.Data.OleDb;

using Engine.Business.Abstract;

namespace Engine.CSV
{
    class CsvScanner: Engine.Business.Interfaces.IScanner
    {
        #region MEMBERS
        private string _CsvFilePath;
        
        public string CsvFilePath
        {
            get { return _CsvFilePath; }
            set { _CsvFilePath = value; }
        }


        private string _CsvFolder;
        
        public string CsvFolder
        {
            get { return _CsvFolder; }
            set { _CsvFolder = value; }
        }

        #endregion

        #region CTOR
        public CsvScanner(string fpath, string folder)
        {
            CsvFilePath = fpath;
            CsvFolder = folder;
        }
        #endregion

        #region SCAN
        public IEnumerable<CollectionItem> Scan()
        {
            OleDbConnection conn = new OleDbConnection();
            conn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + CsvFolder + ";Extended Properties=\"text;HDR=No;FMT=Delimited\"";

            conn.Open();
            OleDbCommand myCommand = conn.CreateCommand();
            string commandString = "select * from [" + CsvFilePath + "]";
            myCommand.CommandType = CommandType.Text;      
            myCommand.CommandText = commandString;       
            OleDbDataReader dataReader = myCommand.ExecuteReader();
            int descj = 0;
            string[] par;
            while(dataReader.Read())
            {
                Engine.Business.Abstract.CollectionItem ci = 
                    Engine.Factory.CollectionFactory.CreateNewCollectionItem();

                string name = dataReader[0].ToString();
                long size = long.Parse(dataReader[1].ToString());
                long crc = long.Parse(dataReader[2].ToString(),System.Globalization.NumberStyles.HexNumber);
                string desc = "";
                if (dataReader.FieldCount >= 4)
                {
                    descj = 3;
                    for(descj = 3; descj < dataReader.FieldCount ;++descj) 
                    {
                        if(dataReader[descj] != System.DBNull.Value)
                            desc += dataReader[descj].ToString() + ",";
                    }
                    
                }
                if(desc.EndsWith(","))
                    desc = desc.Substring(0, desc.Length-1);

                ci.Name = name;
                ci.Size = size;
                ci.CRC32 = crc;
                par = desc.Split(new char[]{','});
                if(par.Length > 1 && par[par.Length - 1].StartsWith(Engine.Factory.CollectionFactory.Link))
                {
                    ci.LinkedPath = par[par.Length - 1].Substring(Engine.Factory.CollectionFactory.Link.Length);
                    ci.Description = desc.Substring(0, desc.IndexOf(Engine.Factory.CollectionFactory.Link) - 1);
                }
                else
                    ci.Description = desc;
                yield return ci;
            }
            conn.Close();
        }

        public int Count
        {
            get 
            {
                OleDbConnection conn = new OleDbConnection();
                conn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + CsvFolder + ";Extended Properties=\"text;HDR=No;FMT=Delimited\"";
                conn.Open();
                OleDbCommand myCommand = conn.CreateCommand();
                string commandString = "select count(*) from [" + CsvFilePath + "]";
                myCommand.CommandType = CommandType.Text;
                myCommand.CommandText = commandString;
                OleDbDataReader dataReader = myCommand.ExecuteReader();
                dataReader.Read();
                int c = int.Parse(dataReader[0].ToString());
                conn.Close();
                return c;
            }
        }


        #endregion

    }
}
